package com.example.easyutils.travel;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.easyutils.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RecycleViewTravel extends RecyclerView.Adapter {

    Context context;
    List mList = new ArrayList();
    public RecycleViewTravel(Context travelActivity, List travelList) {
        this.mList = travelList;
        this.context = travelActivity;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.trance_item,parent,false);
        ViewHoder viewHoder = new ViewHoder(view);
        return viewHoder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Map map = (Map)mList.get(position);
        ((ViewHoder)holder).address.setText("地址： "+map.get("addr").toString());
        ((ViewHoder)holder).grade.setText("级别： "+map.get("grade").toString());
        ((ViewHoder)holder).openTime.setText("开放时间： "+map.get("opentime").toString());
        ((ViewHoder)holder).type.setText("类型： "+map.get("type").toString());
        ((ViewHoder)holder).travelUrl.setText("网址： "+map.get("url").toString());
        ((ViewHoder)holder).visitTime.setText("建议参观时间： "+map.get("visittime").toString());
        ((ViewHoder)holder).tel.setText("拨打电话： "+map.get("tel").toString());

        final String urls = map.get("url").toString();
        final String tels = map.get("tel").toString();
        ((ViewHoder)holder).travelUrl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse(urls);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                context.startActivity(intent);
            }
        });

        ((ViewHoder)holder).tel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + tels);
                intent.setData(data);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHoder extends RecyclerView.ViewHolder {
        public TextView address,grade,openTime,spot,tel,type,travelUrl,visitTime;
        public ViewHoder(@NonNull View itemView) {
            super(itemView);

            address = (TextView)itemView.findViewById(R.id.address);
            grade = (TextView)itemView.findViewById(R.id.grade);
            openTime = (TextView)itemView.findViewById(R.id.openTime);
            spot = (TextView)itemView.findViewById(R.id.spot);
            tel = (TextView)itemView.findViewById(R.id.tel);
            type = (TextView)itemView.findViewById(R.id.type);
            travelUrl = (TextView)itemView.findViewById(R.id.url);
            visitTime = (TextView)itemView.findViewById(R.id.visittime);
        }
    }
}
